home *** CD-ROM | disk | FTP | other *** search
/ Personal Computer World 2008 February / PCWFEB08.iso / Software / Freeware / Miro 1.0 / Miro_Installer.exe / xulrunner / python / psyco / support.py < prev   
Encoding:
Python Source  |  2007-04-13  |  6.1 KB  |  194 lines

  1. ###########################################################################
  2. #  Psyco general support module.
  3. #   Copyright (C) 2001-2002  Armin Rigo et.al.
  4.  
  5. """Psyco general support module.
  6.  
  7. For internal use.
  8. """
  9. ###########################################################################
  10.  
  11. import sys, _psyco, __builtin__
  12.  
  13. error = _psyco.error
  14. class warning(Warning):
  15.     pass
  16.  
  17. _psyco.NoLocalsWarning = warning
  18.  
  19. def warn(msg):
  20.     from warnings import warn
  21.     warn(msg, warning, stacklevel=2)
  22.  
  23. #
  24. # Version checks
  25. #
  26. __version__ = 0x010502f0
  27. if _psyco.PSYVER != __version__:
  28.     raise error, "version mismatch between Psyco parts, reinstall it"
  29.  
  30. version_info = (__version__ >> 24,
  31.                 (__version__ >> 16) & 0xff,
  32.                 (__version__ >> 8) & 0xff,
  33.                 {0xa0: 'alpha',
  34.                  0xb0: 'beta',
  35.                  0xc0: 'candidate',
  36.                  0xf0: 'final'}[__version__ & 0xf0],
  37.                 __version__ & 0xf)
  38.  
  39.  
  40. VERSION_LIMITS = [0x02020200,   # 2.2.2
  41.                   0x02030000,   # 2.3
  42.                   0x02040000]   # 2.4
  43.  
  44. if ([v for v in VERSION_LIMITS if v <= sys.hexversion] !=
  45.     [v for v in VERSION_LIMITS if v <= _psyco.PYVER  ]):
  46.     if sys.hexversion < VERSION_LIMITS[0]:
  47.         warn("Psyco requires Python version 2.2.2 or later")
  48.     else:
  49.         warn("Psyco version does not match Python version. "
  50.              "Psyco must be updated or recompiled")
  51.  
  52. PYTHON_SUPPORT = hasattr(_psyco, 'turbo_code')
  53.  
  54.  
  55. if hasattr(_psyco, 'ALL_CHECKS') and hasattr(_psyco, 'VERBOSE_LEVEL'):
  56.     print >> sys.stderr, ('psyco: running in debugging mode on %s' %
  57.                           _psyco.PROCESSOR)
  58.  
  59.  
  60. ###########################################################################
  61. # sys._getframe() gives strange results on a mixed Psyco- and Python-style
  62. # stack frame. Psyco provides a replacement that partially emulates Python
  63. # frames from Psyco frames. The new sys._getframe() may return objects of
  64. # a custom "Psyco frame" type, which is a subtype of the normal frame type.
  65. #
  66. # The same problems require some other built-in functions to be replaced
  67. # as well. Note that the local variables are not available in any
  68. # dictionary with Psyco.
  69.  
  70.  
  71. class Frame:
  72.     pass
  73.  
  74.  
  75. class PythonFrame(Frame):
  76.  
  77.     def __init__(self, frame):
  78.         self.__dict__.update({
  79.             '_frame': frame,
  80.             })
  81.  
  82.     def __getattr__(self, attr):
  83.         if attr == 'f_back':
  84.             try:
  85.                 result = embedframe(_psyco.getframe(self._frame))
  86.             except ValueError:
  87.                 result = None
  88.             except error:
  89.                 warn("f_back is skipping dead Psyco frames")
  90.                 result = self._frame.f_back
  91.             self.__dict__['f_back'] = result
  92.             return result
  93.         else:
  94.             return getattr(self._frame, attr)
  95.  
  96.     def __setattr__(self, attr, value):
  97.         setattr(self._frame, attr, value)
  98.  
  99.     def __delattr__(self, attr):
  100.         delattr(self._frame, attr)
  101.  
  102.  
  103. class PsycoFrame(Frame):
  104.  
  105.     def __init__(self, tag):
  106.         self.__dict__.update({
  107.             '_tag'     : tag,
  108.             'f_code'   : tag[0],
  109.             'f_globals': tag[1],
  110.             })
  111.  
  112.     def __getattr__(self, attr):
  113.         if attr == 'f_back':
  114.             try:
  115.                 result = embedframe(_psyco.getframe(self._tag))
  116.             except ValueError:
  117.                 result = None
  118.         elif attr == 'f_lineno':
  119.             result = self.f_code.co_firstlineno  # better than nothing
  120.         elif attr == 'f_builtins':
  121.             result = self.f_globals['__builtins__']
  122.         elif attr == 'f_restricted':
  123.             result = self.f_builtins is not __builtins__
  124.         elif attr == 'f_locals':
  125.             raise AttributeError, ("local variables of functions run by Psyco "
  126.                                    "cannot be accessed in any way, sorry")
  127.         else:
  128.             raise AttributeError, ("emulated Psyco frames have "
  129.                                    "no '%s' attribute" % attr)
  130.         self.__dict__[attr] = result
  131.         return result
  132.  
  133.     def __setattr__(self, attr, value):
  134.         raise AttributeError, "Psyco frame objects are read-only"
  135.  
  136.     def __delattr__(self, attr):
  137.         if attr == 'f_trace':
  138.             # for bdb which relies on CPython frames exhibiting a slightly
  139.             # buggy behavior: you can 'del f.f_trace' as often as you like
  140.             # even without having set it previously.
  141.             return
  142.         raise AttributeError, "Psyco frame objects are read-only"
  143.  
  144.  
  145. def embedframe(result):
  146.     if type(result) is type(()):
  147.         return PsycoFrame(result)
  148.     else:
  149.         return PythonFrame(result)
  150.  
  151. def _getframe(depth=0):
  152.     """Return a frame object from the call stack. This is a replacement for
  153. sys._getframe() which is aware of Psyco frames.
  154.  
  155. The returned objects are instances of either PythonFrame or PsycoFrame
  156. instead of being real Python-level frame object, so that they can emulate
  157. the common attributes of frame objects.
  158.  
  159. The original sys._getframe() ignoring Psyco frames altogether is stored in
  160. psyco._getrealframe(). See also psyco._getemulframe()."""
  161.     # 'depth+1' to account for this _getframe() Python function
  162.     return embedframe(_psyco.getframe(depth+1))
  163.  
  164. def _getemulframe(depth=0):
  165.     """As _getframe(), but the returned objects are real Python frame objects
  166. emulating Psyco frames. Some of their attributes can be wrong or missing,
  167. however."""
  168.     # 'depth+1' to account for this _getemulframe() Python function
  169.     return _psyco.getframe(depth+1, 1)
  170.  
  171. def patch(name, module=__builtin__):
  172.     f = getattr(_psyco, name)
  173.     org = getattr(module, name)
  174.     if org is not f:
  175.         setattr(module, name, f)
  176.         setattr(_psyco, 'original_' + name, org)
  177.  
  178. _getrealframe = sys._getframe
  179. sys._getframe = _getframe
  180. patch('globals')
  181. patch('eval')
  182. patch('execfile')
  183. patch('locals')
  184. patch('vars')
  185. patch('dir')
  186. patch('input')
  187. _psyco.original_raw_input = raw_input
  188. __builtin__.__in_psyco__ = 0==1   # False
  189.  
  190. if hasattr(_psyco, 'compact'):
  191.     import kdictproxy
  192.     _psyco.compactdictproxy = kdictproxy.compactdictproxy
  193.